package com.dimong.www.loan.ui.act;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.dimong.www.loan.R;
import com.dimong.www.loan.ui.base.BaseRefreshActivity;
import com.dimong.www.loan.widget.ActionBarView;

/**
 * Created by wuligang on 16/8/30.
 */
public class WebViewAty extends BaseRefreshActivity {
    private ActionBarView title_web_view;
    private WebView wv_common_html;
    private String url;
    private String title;

    @Override
    public void initIntentParam(Intent intent) {
    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        title_web_view = findViewById(R.id.title_web_view);
        wv_common_html = findViewById(R.id.wv_common_html);
    }

    @Override
    public void setViewStatus() {
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
        if (TextUtils.isEmpty(url)) {
            finish();
            return;
        }
        if(TextUtils.isEmpty(title)){
            title_web_view.setVisibility(View.GONE);
        }else {
            title_web_view.setTitle(title);
            title_web_view.setLeftImg(new ActionBarView.OnLeftImgClickListener() {
                @Override
                public void onClick(View button) {
                    finish();
                }
            });
        }
        wv_common_html.getSettings().setJavaScriptEnabled(true);
        wv_common_html.getSettings().setDefaultTextEncodingName("UTF-8");
        wv_common_html.setWebViewClient(new DetailWebViewClient());
        wv_common_html.getSettings().setUseWideViewPort(true);
        wv_common_html.getSettings().setLoadWithOverviewMode(true);
        wv_common_html.setWebChromeClient(new DetailWebChromeClient());
        wv_common_html.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            wv_common_html.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        startRefresh();
    }

    @Override
    public void finish() {
        if (wv_common_html.canGoBack()) {
            wv_common_html.goBack();
        } else {
            super.finish();
        }
    }

    @Override
    public void onActResume() {

    }

    @Override
    public void onActPause() {

    }

    @Override
    public void onActDestroy() {

    }

    @Override
    public void onActRestart() {

    }

    @Override
    public void onActStop() {

    }

    @Override
    public void onActStart() {

    }

    @Override
    public int getContentView() {
        return R.layout.act_common_html;
    }

    @Override
    public RelativeLayout getContainView() {
        return findViewById(R.id.rv_activity_common_html);
    }

    private class DetailWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            showRefreshSuccess();
            wv_common_html.setVisibility(View.VISIBLE);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }

    private class DetailWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
        }
    }

    private void startRefresh() {
        showRefreshLoading();
        wv_common_html.loadUrl(url);
    }
}